package com.happyfamily.springboot.crowdsourcingplatform.controller.weng;

import com.happyfamily.springboot.crowdsourcingplatform.annotation.OperationLogDetail;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.PageTableRequest;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.dto.LatestInfoDto;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationType;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationUnit;
import com.happyfamily.springboot.crowdsourcingplatform.model.LastestInfomation;
import com.happyfamily.springboot.crowdsourcingplatform.service.weng.LastInfoService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@Slf4j
public class LatestInformationController
{
	@Autowired
	LastInfoService lastInfoService;

	@GetMapping("/api-back/latestInfo/list")
	@ResponseBody
	@ApiOperation(value = "后台分页获取最新咨询", notes = "后台分页获取最新咨询", httpMethod = "GET", response = Results.class)
	@OperationLogDetail(detail = "分页获取最新资讯", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	public Results<LastestInfomation> getAllLastInfoByPage(PageTableRequest request)
	{
		request.countOffset();
		return lastInfoService.getAllLastInfoByPage(request.getOffset(), request.getLimit());
	}


	@GetMapping("/api-back/latestInfo/delete")
	@ResponseBody
	@PreAuthorize("hasAuthority('back:news:delete')")
	@ApiOperation(value = "通过ID删除最新资讯", notes = "通过ID删除最新资讯", httpMethod = "GET", response = Results.class)
	@OperationLogDetail(detail = "删除最新资讯", level = 4, operationUnit = OperationUnit.ADMIN, operationType = OperationType.DELETE)
	public Results<LastestInfomation> delete(LatestInfoDto latestInfoDto)
	{
		lastInfoService.deleteLatestInfo(latestInfoDto.getLid());
		return Results.success();
	}

	@GetMapping("/api-back/latestInfo/open")
	@ResponseBody
	@PreAuthorize("hasAuthority('back:news:update')")
	@ApiOperation(value = "通过ID发布最新资讯", notes = "通过ID发布最新资讯", httpMethod = "GET", response = Results.class)
	@OperationLogDetail(detail = "发布最新资讯", level = 2, operationUnit = OperationUnit.ADMIN, operationType = OperationType.UPDATE)
	public Results<LastestInfomation> open(LatestInfoDto latestInfoDto)
	{
		lastInfoService.openLatestInfo(latestInfoDto.getLid());
		return Results.success();
	}

	@GetMapping("/api-back/latestInfo/close")
	@ResponseBody
	@PreAuthorize("hasAuthority('back:news:update')")
	@ApiOperation(value = "通过ID下架最新资讯", notes = "通过ID下架最新资讯", httpMethod = "GET", response = Results.class)
	@OperationLogDetail(detail = "下架最新资讯", level = 2, operationUnit = OperationUnit.ADMIN, operationType = OperationType.UPDATE)
	public Results<LastestInfomation> close(LatestInfoDto latestInfoDto)
	{
		lastInfoService.closeLatestInfo(latestInfoDto.getLid());
		return Results.success();
	}

	@GetMapping("/api-back/latestInfo/findLatestInfoByFuzzy")
	@ResponseBody
	@PreAuthorize("hasAuthority('back:news:query')")
	@ApiOperation(value = "通过条件模糊查询最新资讯", notes = "通过条件模糊查询最新资讯", httpMethod = "GET", response = Results.class)
	@OperationLogDetail(detail = "模糊查询最新资讯", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	public Results<LastestInfomation> getInfoByFuzzy(PageTableRequest requests, String ltitle, String getDate)
	{
		requests.countOffset();
		return lastInfoService.getInfoByFuzzy(ltitle, getDate, requests.getOffset(), requests.getLimit());
	}

	@GetMapping("/api-back/latestInfo/edit")
	@ResponseBody
	@PreAuthorize("hasAuthority('back:news:update')")
	@ApiOperation(value = "通过ID修改资讯标题", notes = "通过ID修改资讯标题", httpMethod = "GET", response = Results.class)
	@OperationLogDetail(detail = "修改资讯标题", level = 2, operationUnit = OperationUnit.ADMIN, operationType = OperationType.UPDATE)
	public Results<LastestInfomation> infoEdit(LatestInfoDto latestInfoDto)
	{
		lastInfoService.infoEdit(latestInfoDto.getLid(), latestInfoDto.getLtitle());
		return Results.success();
	}

	@GetMapping("/latestInfo/frontGetLatestNews")
	@ResponseBody
	@ApiOperation(value = "主页面获取可上架的最新资讯", notes = "主页面获取可上架的最新资讯", httpMethod = "GET", response = List.class)
	@OperationLogDetail(detail = "主页面获取可上架的最新资讯", level = 1, operationUnit = OperationUnit.SYSTEM, operationType = OperationType.SELECT)
	public List<LastestInfomation> frontGetLatestNews()
	{
		return lastInfoService.frontGetLatestNews();
	}


}
